//初始化数据
$(function () {
    getData(1,6);
})
//更新数据
function getData(pageNo,length) {
    $.ajax({
        url: "/activity/list",
        type: "POST",
        dataType: "json",
        data:getSearch(pageNo,length),
        success:function (json) {
            var str="";
            var dataList = json.data.list;
            createPageNav({
                $container : $("#pageing2"),
                pageCount :json.data.pages,
                currentNum : json.data.pageNum,
                afterFun : function(num){
                    getData(num,6);
                },
                hasCommonPage:false
            });

            for (var i = 0; i < dataList.length; i++) {
                var routeType =dataList[i].routeType;
                var status = dataList[i].status;
                if (routeType ==0){routeType="省内活动"}
                if (routeType ==1){routeType="省外活动"}
                if (routeType ==2){routeType="国外活动"}
                if (status ==0){status="未发布"}
                if (status ==1){status="发布"}
                if (status ==2){status="下架"}
                var date = "";
                date = new Date(dataList[i].gmtModify);
                str += "<tr>"
                    + '<th>' + dataList[i].id + '</th>'
                    +"<th ><img src='"+dataList[i].img+"' class='routeImg'></th>"
                    + "<th>" + dataList[i].routeName + "</th>"
                    + "<th>" + routeType + "</th>"
                    + "<th>" + status + "</th>"
                    + "<th>" + formatDate(dataList[i].updateTime)+ "</th>"
                    + "<th>"
                    +"<input type='button' value='修改主图' class='btn btn-info btn-sm'  data-toggle='modal' data-target='#imgModal'"+"data-whatever='" +  dataList[i].id + "'/>"
                    +"<input type='button' value='活动编辑' class='btn btn-success btn-sm'  data-toggle='modal' data-target='#exampleModal'"+"data-whatever='" +  dataList[i].id + "'/>"
                    +"<input type='button' value='订单编辑' class='btn btn-success btn-sm'  data-toggle='modal' data-target='#orderModal'"+"data-whatever='" +  dataList[i].id + "'/>"
                    +"<input type='button' value='删除'  onclick='deleteById("+dataList[i].id+")'  class='btn btn-danger btn-sm'/>"
                    +"</th>"
                    + "</tr>"
            }
            $('#list').html(str);
        },
        error:function () {
            alert("操作失败");
        }
    })
}
//删除数据
function deleteById(id) {
    $.ajax({
        url: "/activity/delete",
        type: "POST",
        dataType: "json",
        data:{id:id},
        success:function (json) {
            alert("成功");
            getData(1,6);
        },
        error:function () {
            alert("操作失败");
        }
    })
}
//提交修改活动内容
function updataData() {
    var routeName = $('#routenames').val();
    var title = $('#title').val();
    var detail = $('#detail').val();
    var cost = $('#cost').val();
    var prompt = $('#prompt').val();
    var prompt = $('#prompt').val();
    $.ajax({
        url:"/activity/updata",
        type:"POST",
        dataType:"json",
        data:{id:recipient,routeName:routeName,title:title,detail:detail,cost:cost,prompt:prompt},
        success:function (json) {
            alert("提交成功");
            $("#exampleModal").modal("hide");
            getData(1,6);
        },error:function () {
            alert("提交失败，必须存在一条权限")
        }
    })
}
//提交修改订单内容
function updataOrderData() {
    var id = $('#ide').val();
    var date = $('#date').val();
    var adultPrice = $('#adultPrice').val();
    var childPrice = $('#childPrice').val();
    var dateTime = $('#dateTime').val();
    var stock = $('#stock').val();
    var collectionSite = $('#collectionSite').val();
    var tel = $('#tel').val();
    $.ajax({
        url:"/activity/updata",
        type:"POST",
        dataType:"json",
        data:{id:id,date:date,adultPrice:adultPrice,childPrice:childPrice,stock:stock,collectionSite:collectionSite,tel:tel,dateTime:dateTime},
        success:function (json) {
            alert("提交成功");
            $("#orderModal").modal("hide");
            getData(1,6);
        },error:function () {
            alert("提交失败，必须存在一条权限")
        }
    })
}
//提交修改主图
function updataDataImage() {
    var formData = new FormData();
    formData.append("id",recipient);
    formData.append("file",$('#file')[0].files[0]);
    $.ajax({
        url:"/activity/updata",
        type:"POST",
        dataType:"json",
        data:formData,
        processData:false,
        contentType:false,
        success:function (json) {
            alert("修改成功");
            $("#imgModal").modal("hide");
            getData(1,6);
        },error:function () {
            alert("提交失败，必须存在一条权限")
        }
    })
}
//获取检索条件
function getSearch(pageNo,length) {
    var routeName = $('[name="routeName"]').val();
    var status = $('[name="status"]').val();
    var routeType = $('[name="routeType"]').val();
    var starttime = $('[name="starttime"]').val();
    var endtime = $('[name="endtime"]').val();
    var parm={pageNo:pageNo,length:length,routeName:routeName,routeType:routeType,status:status,starttime:starttime,endtime:endtime};
    return parm;
}
//时间戳转系统时间格式
function formatDate(now) {
    if (now==""){return "";}
    return new Date(now).toLocaleString();
}